<?php
class Tab{
	
	/* public */
		var $attrs;
		var $page;
	/* private */
		var $SkipPower;
		var $power;
	
	/* public */
	function __construct($id, $skip = false){
		$id	= (int) $id;
		include( B_DIR . '/_res/form/' . $id . '.php');
		$this->attrs	=& $T;
		if( $skip || empty(U::$tabPower[$id]) ){
			$this->SkipPower	= true;
		}else{
			if( is_array(U::$tabPower[$id]) ){
				$this->power	=& U::$tabPower[$id];
				/*
				foreach($this->power as $fid=>&$vals){
					foreach($this->attrs['field'] as &$field){
						if( $field['id'] == $fid ){
							foreach($field['child'] as $val=>&$txt){
								if( !$vals[$val] ){
									unset($field['child'][$val]);
								}
							}
							unset($txt);
							break;
						}
					}
					unset($field);
				}
				*/
			}
			unset($vals);
		}
	}

	
	/* private */
	function validate(&$L, $_updates = false){

		if( $this->SkipPower ){
			return true;
		}
		if( is_array($L) ){
			foreach($this->attrs['field'] as $field =>&$f){
				$fid	=  $f['id'];
				if( $this->power[$fid] ){
					if( empty($this->power[$fid][$L[$field]] ) ){					
						if( class_exists('A') ){
							A::msg('权限检测失败！', '', 3, 3);
						}else{
							die('Power validate Failure On File:' . __FILE__ . ' Line:' . __LINE__ );
						}
					}
				}
			}
			unset($f);
		}

		if( is_array($_updates) ){
			foreach($_updates as $field){
				$fid	=  $this->attrs['field'][$field]['id'];
				if( $this->power[$fid] ){
					if( empty($this->power[$fid][$_POST[$field]] ) ){
						if( class_exists('A') ){
							A::msg('权限检测失败！', '', 3, 3);
						}else{
							die('Power validate Failure On File:' . __FILE__ . ' Line:' . __LINE__ );
						}
					}
				}
			}
		}
		return true;
	}


	function deCode(&$items){
		if( empty($items) ){
			return $items;
		}
		foreach($items as $k=>&$v){
			if( $this->attrs['field'][$k] ){
				switch ($this->attrs['field'][$k]['elementType']) {
					case 'richtext':
						if( $v ) {
							$v	=& str_replace(  '"/__up__/', '"' . W_DIR . '/_res/_up/', $v);
						}
						break;
					case 'file':
						if( $v && strpos($v, '/_res/_up/')===false ) {
							$v	= W_DIR . '/_res/_up/' . $v;
						}
						break;

					case 'checkbox':
						if( $v ) {
							$items[$k]	= explode(',', $v);
						}
						break;

				}
			}
		}
		unset($v);
	}

	function deCodeList(&$L){
		foreach($L as &$v){
			$this->deCode($v);
		}
	}


	function & One($id){
		$id	= (int) $id;
		$L	= E::$DB->one($this->attrs['tableName'], 'id=' . $id);
		$this->deCode($L);
		return $L;
	}


	function &all($o = '*' , $if = 1){
		$L	= E::$DB->ary($this->attrs['tableName'], $if, $o );
		return $L;
	}



	function &ary($o = '*', $if = 1, $iv = false , $ik = 'id'){
		$L	=& $this->all($o, $if);
		if( empty( $L ) ){
			return $L;
		}
		$R	= array();
		foreach($L as $k=> &$v){
			$R[$v[$ik]]	= $iv ? $v[$iv] : $v;
		}
		unset($v);
		unset($L);
		return $R;
	}

	function & page($o , $if, $ppl = 24, $lang = 1){
		$this->page	=& E::$DB->page($this->attrs['tableName'], $if) ;
		$L		=& $this->ary($o , $if . ' ' . $this->page['limit']);
		return $L;
	}


	function & delete($id){
		$L	= $this->one($id);
		$_L	= E::$DB->one($this->attrs['tableName'], 'id=' . $id);
		if(  $this->validate($L) ){
			$id	= (int) $L['id'];
			foreach($this->attrs['field'] as $key => $f){
				if ( $f['elementType'] === 'file' ) {
					if( !empty( $L[$key] ) ){
						$_f	= B_DIR . '/_res/_up/' .$_L[$key];
						@unlink( $_f );
					}
				}
			}
			$_dir	= B_DIR . '/_res/_up/' . $this->attrs['id'] . '_' . $id ;
			VFS::deldir( $_dir );
			E::$DB->query('DELETE FROM ' . $this->attrs['tableName'] . ' WHERE id=' . $id);
		}else{
			die('权限检测失败！');
		}
		return $L;
	}


	function & insert($add){
		$_updates	= array();
		$_files		= array();

		foreach($this->attrs['field'] as $key => $f){
			switch ($f['elementType']) {
					case 'file':
						array_push($_files, $key);
						break;
					case 'richtext':
						self::htmlEnCode($_POST[$key]);
						array_push($_updates, $key);
						break;
					case 'checkbox':
						$_POST[$key]	= join(',', $_POST[$key]);
						array_push($_updates, $key);
						break;
					case 'date':
						$_tmp	= preg_split("/\D/", $_POST[$key]);
						$_POST[$key]	= mktime( 0, 0, 0, $_tmp[1], $_tmp[2], $_tmp[0]);
						array_push($_updates, $key);
						break;
					default:
						array_push($_updates, $key);

			}
		}
		$add		= explode(',', $add);
		foreach($add as $v){
			$v	= trim($v);
			if( $v ){
				array_push($_updates, $v);
			}
		}
		if( empty($_updates) ){
			die( 'Error On File:' . __FILE__ . ' Line:' . __LINE__);
		}
		$id	= false;
		if( $this->validate($tmp = false, $_updates) ) {
			$id	= E::$DB->insert($this->attrs['tableName'], $_updates);
			if( !empty($_files) ) {
				$this->__files($_files, $id);
			}
		}else{
			die('权限检测失败！');
		}
		return $id ;
	}


	function & update($add, $id){

		$L	= $this->one($id);

		$this->validate($L);

		if( empty($L['id']) ){
			die( 'Error On File:' . __FILE__ . ' Line:' . __LINE__);
		}
		
		$_updates	= array();
		$_files		= array();
		$_encodes	= array();


		foreach($this->attrs['field'] as $key =>&$f){
			if( !$f['isEdit'] ){
					continue;
			}
			switch ($f['elementType']) {
					case 'file':
						array_push($_files , $f['fieldKey'] );
						break;
					case 'richtext':
						self::htmlEnCode($_POST[$key]);
						array_push($_updates , $f['fieldKey'] );
						break;
					case 'checkbox':
						if( is_array($_POST[$key]) ){
							$_POST[$key]	= join(',', $_POST[$key]);
							array_push($_updates , $f['fieldKey'] );
						}
						break;
					case 'date':
						$_tmp	= preg_split("/\D/", $_POST[$key]);
						$_POST[$key]	= mktime( 0, 0, 0, $_tmp[1], $_tmp[2], $_tmp[0]);
						array_push($_updates, $key);
						break;
					default:
						array_push($_updates , $f['fieldKey'] );
			}
		}
		unset($f);

		$add	= explode(',', $add);
		foreach($add as $v){
			$v	= trim($v);
			if( $v ){
				array_push($_updates, $v);
			}
		}
		if( !empty($_updates) ){
			if( $this->validate($L, $_updates) ) {
				E::$DB->update($this->attrs['tableName'], $_updates, ' id = '. $L['id']);
			}else{
				die('权限检测失败！');
			}
		}

		if( !empty($_files) ) {
			$this->__files($_files, $L['id']);
		}
		return $L;
	}


	/* private */
	function __files( $_files, $id){
		$id	= (int) $id;
		$udir	=  $this->attrs['id'] . '_T';
		$_ups	= array();
		foreach($_files  as $key){
			$file	=& $_FILES[$key];
			$field	=& $this->attrs['field'][$key];
			if(  !empty($file['tmp_name']) && ( $field['elementType'] === 'file' ) ){
				if ( 
					!preg_match(  '/' . $field['regs'] . '/i' , $file['name'] )
				){
					die( 'Error On File:' . __FILE__ . ' Line:' . __LINE__);
				}
					
				if( !is_dir( B_DIR. '/_res/_up/' . $udir ) ){
					@mkdir( B_DIR. '/_res/_up/' . $udir , 0755) or die( 'Error On File:' . __FILE__ . ' Line:' . __LINE__);
				}

				$_POST[$key]	=	$udir . '/'. $id  .'_'. $field['id'] . strstr($file['name'] , '.');
				$to		=	B_DIR . '/_res/_up/' . $_POST[$key];
				@unlink($to);
				move_uploaded_file($file['tmp_name'], $to ) or die( 'Error On File:' . __FILE__ . ' Line:' . __LINE__);
				array_push($_ups, $key);
			}
		}
		if( !empty($_ups) ){
			E::$DB->update($this->attrs['tableName'], $_ups, ' id = '. $id);
		}
	}

	static function htmlEnCode(&$html){
			$html	= str_replace( '"' . W_DIR  . '/_res/_up/', '"/__up__/', $html);
			$html	= preg_replace("/\<br\s*\/?\>\s*<br\s*\/?\>\s*/i", "<p>", $html);
			$html	= preg_replace("/\<br\s*\/?\>(\s*&nbsp\;\s*)+<br\s*\/?\>(\s*&nbsp\;\s*)+/i", "<p>", $html);
			$html	= preg_replace("/\<p\>\s*　\s*　\s*/i", "<p>", $html);
			$html	= preg_replace("/\<p\>(\&nbsp\;\s?)+/i", "<p>", $html);
			$html	= preg_replace("/\<p\>\s+/i", "<p>", $html);
			$html	= preg_replace("/\s+on[a-z]+=.+?\>/i", ">", $html );
			$html	= preg_replace("/\<script.+?\<\/script\>/i", "", $html);

	}
}

?>